package cn.bytecollege.controller;



import cn.bytecollege.entity.Room;
import cn.bytecollege.entity.RoomCategory;
import cn.bytecollege.service.RoomCategoryService;
import cn.bytecollege.service.RoomService;


import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * @author LH
 * @time 2021-10-28 14:58
 *
 * 通过ajax将输入的房间号提交，用来判断是否存在，是否已经有人入住
 */
@WebServlet(name = "CheckIdNumber", value = "/checkRoomNumber")
public class CheckRoomNumberController extends HttpServlet {
    String msg=null;
    private RoomService roomService=null;
    private RoomCategoryService roomCategoryService=null;
    public CheckRoomNumberController(){
         roomService=new RoomService();
         roomCategoryService=new RoomCategoryService();
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String roomNumber=request.getParameter("roomNumber");
        Room room=null;
        room=roomService.findByRoomNumber(Integer.valueOf(roomNumber));
        if (room!=null){
            //说明该room可以入住
            Integer roomCategoryId= room.getRoomCategoryId();
            RoomCategory roomCategory=roomCategoryService.findByRoomCategoryId(roomCategoryId);
            String roomCategoryName=roomCategory.getRoomCategoryName();
            msg="该房间类型为";
            response.getWriter().write(msg);
            response.getWriter().write(roomCategoryName);
            String roomPrice="价钱为";
            Double price =room.getRoomPrice();
            response.getWriter().write("   ");
            response.getWriter().write(roomPrice);
            response.getWriter().write(String.valueOf(price));
            return;
        }
        //执行到这说明此间房已有人入住或不存在。
        msg="本间房不存在或已有人入住";
        response.getWriter().write(msg);
    }
}
